/**
 * 和utils里的index.js里方法一致
 * @param {string} url
 * @returns {Object}
 */
function param2Obj(url) {
  // 假如url："http://localhost:8080/a/b/c?a=10&b=20&c=30"
  // decodeURIComponent()URL解码%E5%E3 转成中文
  // 后面如果参数有a=10+8&b=20+3 转成 "a=10 8&b=20 3"
  const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  // search变量：a=10 &b=20 &c=30
  if (!search) {
    return {}
  }
  const obj = {}
  const searchArr = search.split('&')
  searchArr.forEach(v => {
    const index = v.indexOf('=')
    if (index !== -1) {
      const name = v.substring(0, index)
      const val = v.substring(index + 1, v.length)
      obj[name] = val
    }
  })
  return obj
}

module.exports = {
  param2Obj
}
